package edu.xhu.communityantiepidemicsystemback.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import edu.xhu.communityantiepidemicsystemback.common.result.PageResult;
import edu.xhu.communityantiepidemicsystemback.common.result.Result;
import edu.xhu.communityantiepidemicsystemback.dto.*;
import edu.xhu.communityantiepidemicsystemback.entity.Community;
import edu.xhu.communityantiepidemicsystemback.service.ICommunityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lcx
 * @since 2022-06-20
 */
@RestController
@RequestMapping("/community")
public class CommunityController {

    @Autowired
    private ICommunityService communityService;

    @PostMapping("/all")
    public PageResult<?> allCommunityInfo(@RequestBody Query query){
        IPage<Community> communityIPage= communityService.getAllCommunityInfo(query);
        if(Objects.isNull(communityIPage)){
            return new PageResult<>();
        }
        return new PageResult<>(communityIPage.getTotal(),communityIPage.getRecords());
    }

    @PostMapping("/fuzzysearch")
    public PageResult<?> fuzzyCommunityInfo(@RequestBody Fuzzy fuzzy){
        IPage<Community> communityIPage= communityService.fuzzyCommunityInfo(fuzzy);
        if(Objects.isNull(communityIPage)){
            return new PageResult<>();
        }
        return new PageResult<>(communityIPage.getTotal(),communityIPage.getRecords());
    }

    @PostMapping("/edit")
    public Result<?> EditCommunityInfo(@RequestBody UpdateCommunity updateCommunity){
        boolean result= communityService.editCommunityInfo(updateCommunity);
        if(result){
            return new Result<>().success();
        }else{
            return new Result<>().error("修改失败,服务器内部错误");
        }
    }

    @PostMapping("/add")
    public Result<?> AddCommunityInfo(@RequestBody CreateCommunity createCommunity){
        boolean result= communityService.addCommunityInfo(createCommunity);
        if(result){
            return new Result<>().success();
        }else{
            return new Result<>().error("添加失败,服务器内部错误");
        }
    }

    @GetMapping("/del/{id}")
    public Result<?> DelCommunityInfo(@PathVariable("id") Integer id){
        boolean result = communityService.delCommunityInfo(id);
        if(result){
            return new Result<>().success();
        }
        return new Result<>().error("删除错误,服务器内部错误");
    }


}
